//
//  BaseViewController.m
//  LiMinProject
//
//  Created by FM123 on 2017/9/28.
//  Copyright © 2017年 LiMin. All rights reserved.
//

#import "BaseViewController.h"
#import "UIBarButtonItem+Extension.h"
@interface BaseViewController ()<UITabBarControllerDelegate>
{
    
    BOOL isDoubleClick;
    NSDate*lastDate;
    
}
@end

@implementation BaseViewController

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    }
    return self;
}

- (id)init{
    self = [super init];
    if (self) {
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
        
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.view setBackgroundColor:[UIColor whiteColor]];
    isDoubleClick=YES;
    
    self.tabBarController.delegate=self;
}
-(void)popToControllerWithIndex:(int )index;{
    NSArray * ctrlArray = self.navigationController.viewControllers;
    WWLog(@"ctrlArray  %@",ctrlArray);
    [self.navigationController popToViewController:[ctrlArray objectAtIndex:ctrlArray.count-2] animated:YES];
    
}
-(void)backActionToViewController:(UIViewController *)controller;{
//    WWLog(@"%@  %@",controller.className,controller.nextResponder.classNam/Users/ellen/Desktop/测试/LiMinProject/Pods/YYKite);
    UINavigationController *navigationVC = self.navigationController;
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    //    遍历导航控制器中的控制器
    for (UIViewController *vc in navigationVC.viewControllers) {
        
        [viewControllers addObject:vc];
        //        CourseTableController就是你需要返回到指定的控制器名称，这里我需要跳转到CourseTableController这个控制器
        if ([vc isKindOfClass:[controller.className class]]) {
            break;
        }
    }
    //    把控制器重新添加到导航控制器
    [navigationVC setViewControllers:viewControllers animated:YES];
    [navigationVC popViewControllerAnimated:YES];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    self.navigationItem.leftBarButtonItem=[UIBarButtonItem
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImage:@"返回黑" andDirection:1 target:self action:@selector(leftClick)];
    
}
-(void)leftClick{
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
   
     BaseViewController *vc = tabBarController.selectedViewController;
    NSDate *date = [[NSDate alloc] init];
    
     if ([vc isEqual:viewController]) {
             // 处理双击事件
         if (date.timeIntervalSince1970 - lastDate.timeIntervalSince1970 < 0.5) {
             [[NSNotificationCenter defaultCenter]postNotificationName:@"clickTabBar" object:nil];
//             if (_baseDelegate&&[_baseDelegate respondsToSelector:@selector(clickTabBar)]) {
//
//                 [_baseDelegate clickTabBar];
//             }
//
         }
        
             lastDate = date;
        
             return NO;
     }
    
     return YES;
    
    
}
-(void)clickTabBar{
    if (isDoubleClick==YES) {
       
        WWLog(@"点击");
        self.clickTabbar();
        isDoubleClick=NO;
        
    }else{
        isDoubleClick=YES;
    }
    
}
@end
